<?php


namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\common\Selector;
use app\admin\CommonException;
use app\admin\model\GroupPath;
use app\admin\model\Path as PathModel;

class Path extends BaseController
{
    public function index()
    {
        $model = new PathModel();
        $list = $model->getPaths($this->request);
        $select = [
            'id' => '节点编号',
            'path_name' => '节点名称',
            'path' => '节点路径'
        ];
        $selector = Selector::render($select, 'search', $this->request->get('search', ''));
        return view('path/index', [
            'list' => $list->toArray()['data'] ?? [],
            'page' => $list->__toString(),
            'selector' => $selector
        ]);
    }

    /**
     * @return bool|\think\response\View
     * @throws CommonException
     */
    public function add()
    {
        $data = $this->request->post();
        if (!$data) {
            return view('path/add', ['url' => '/path/add']);
        }

        $validate = new \app\validate\path();
        $result = $validate->check($data);
        if (!$result) error($validate->getError(), '', 10086);

        //新增
        $model = new PathModel();
        $model->data($data, true);

        $res = $model->save();
        if ($res) {
            $this->success('添加成功!', '/path/add');
        }
        error('添加失败!请稍候再试');
        return false;
    }

    /**
     * 删除
     * @throws
     */
    public function del()
    {
        $model = new PathModel();
        $id = $this->request->get('id', '');
        if (!$id) return false;
        $path = $model->find($id);
        if (!$path) return false;
        return $path->delete();
    }

    /**
     * 检查节点名唯一
     */
    public function checkName()
    {
        $name = $this->request->post('path_name');
        $id = $this->request->post('id', 0);
        $validate = new \app\validate\path();
        $data = ['path_name' => $name];
        if ($id) $data['id'] = $id;
        $result = $validate->check($data);
        return $result;
    }

    /**
     * @return \think\response\View
     * @throws
     */
    public function update()
    {
        $data = $this->request->post();
        if (!$data) {
            $id = $this->request->request('id', '');
            if (!$id) throw new CommonException('缺少参数!', '/path/index');
            $path = PathModel::find($id);
            if (!$path) throw new CommonException('请刷新后重试!', '/path/index');
            return view('path/add', ['data' => $path, 'url' => '/path/update']);
        }

        //修改
        $path = PathModel::find($data['id']);
        if (!$path) throw new CommonException('请刷新后重试!', '/path/index');

        $validate = new \app\validate\path();
        $result = $validate->check($data);
        if (!$result) error($validate->getError(), '', 10086);

        //修改
        $path->data($data, true);
        $res = $path->save();
        if ($res) {
            $this->success('修改成功!', '/path/update.html?id='.$data['id']);
        }
        error('修改失败!请稍候再试');
    }

    /**
     * 获取权限组联想路径
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function searchGroupPath(){
        $paths = [];
        $data = $this->request->get();
        $groupId = $data['group_id'];
        $searchWord = trim($data['path']);
        $modelGroup = new \app\admin\model\RoleGroup();
        $group = $modelGroup->find($groupId);
        if($group){
            $groupPaths = GroupPath::where('group_id',$groupId)->column('path_id');
            $paths = PathModel::where('id','not in',$groupPaths)->where('path','like','%'.$searchWord.'%')->select()->toArray();
        }
        return json($paths);
    }

}